package com.wondersgroup.framework.widget.library.extjs.form;

import com.wondersgroup.framework.widget.codegen.CodeGenerator;
import com.wondersgroup.framework.widget.codegen.annotation.CodeMethod;
import com.wondersgroup.framework.widget.codegen.annotation.ObjectField;
import com.wondersgroup.framework.widget.codegen.annotation.ObjectMapping;
import com.wondersgroup.framework.widget.codegen.expression.Expression;
import com.wondersgroup.framework.widget.codegen.expression.ExpressionFactory;
import com.wondersgroup.framework.widget.codegen.expression.JSFactory;
import com.wondersgroup.framework.widget.codegen.process.BaseCodeObject;
import com.wondersgroup.framework.widget.codegen.process.CodeObject;
import com.wondersgroup.framework.widget.library.extjs.ExtStore;

@ObjectMapping("cf_f.ComboBox")
public class ExtGridSelect extends ExtBaseSelectField {

	@ObjectField(index = 1, token = true)
	private String store;

	@ObjectField(index = 2)
	private String triggerAction = "all";

	@ObjectField(index = 3, defaultValue = "4")
	private int minChars;

	@ObjectField(index = 4, defaultValue = "query")
	private String queryParam;

	@ObjectField(index = 5, defaultValue = "0")
	private String listWidth;

	@ObjectField(index = 6, defaultValue = "0")
	private int pageSize;

	@ObjectField(index = 7, token = true)
	private String tpl;

	@ObjectField(index = 8)
	private String itemSelector = "div.search-item";
	private String totalSize;
	private String root;
	private String fields;
	private String title;
	private boolean gridColumn;
	protected final BaseCodeObject<ExtGridSelect> codeObject = new BaseCodeObject<ExtGridSelect>() {
		@CodeMethod(index = 1)
		void gridSelect() {
			ExtStore localExtStore = new ExtStore();
			localExtStore.setUrl(getUrl());
			localExtStore.setUrlParams(getUrlParams());
			if (getRoot() != null) {
				localExtStore.setRoot(getRoot());
			}

			if (getTotalSize() != null) {
				localExtStore.setTotalProperty(getTotalSize());
			}
				
			if ((getPageSize() == 0) && (getRoot() == null)) {
				localExtStore.setRoot(null);
			}
				
			localExtStore.setFields(getFields());
			$JS.var(getId() + "_store", JS.instance(localExtStore));
			$JS.var(getId(), JS.instance(getContext()));
			if (getOnSelect() != null) {
				$JS.method(getId() + ".on", new Expression[] { BASE.json("select"), BASE.token(getOnSelect()) });
			}
				
			if ((getPageSize() <= 0) || (isReadOnly())) {
				$JS.method(getId() + ".on", new Expression[] { BASE.json("beforequery"), BASE.token("setTriggerDisable") });
			}
				
			$JS.method(getId() + ".addEvents", new Expression[] { BASE.json("onclean") });
			$JS.method(getId() + ".on",
					new Expression[] {
							BASE.json("beforequery"),
							BASE.token("function(){if(this.getRawValue()==''){this.fireEvent(\"onclean\", this);}}") });
			$BASE.token("Wonders.Combo.setGridSelectFirstValue('" + getId()+ "','" + getValueField() + "');");
		}
	};

	public String getJS() {
		this.codeObject.setContext(this);
		String str = CodeGenerator.generate(new CodeObject[] { this.codeObject });
		return str.concat(getPushScript());
	}

	public ExtGridSelect(String paramString) {
		super(paramString);
	}

	public String getStore() {
		return this.store;
	}

	public void setStore(String paramString) {
		this.store = paramString;
	}

	public String getTriggerAction() {
		return this.triggerAction;
	}

	public void setTriggerAction(String paramString) {
		this.triggerAction = paramString;
	}

	public int getMinChars() {
		return this.minChars;
	}

	public void setMinChars(int paramInt) {
		this.minChars = paramInt;
	}

	public String getQueryParam() {
		return this.queryParam;
	}

	public void setQueryParam(String paramString) {
		this.queryParam = paramString;
	}

	public String getListWidth() {
		return this.listWidth;
	}

	public void setListWidth(String paramInt) {
		this.listWidth = paramInt;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(int paramInt) {
		this.pageSize = paramInt;
	}

	public String getTotalSize() {
		return this.totalSize;
	}

	public void setTotalSize(String paramString) {
		this.totalSize = paramString;
	}

	public String getRoot() {
		return this.root;
	}

	public void setRoot(String paramString) {
		this.root = paramString;
	}

	public String getFields() {
		return this.fields;
	}

	public void setFields(String paramString) {
		this.fields = paramString;
	}

	public String getTpl() {
		return this.tpl;
	}

	public void setTpl(String paramString) {
		this.tpl = paramString;
	}

	public String getItemSelector() {
		return this.itemSelector;
	}

	public void setItemSelector(String paramString) {
		this.itemSelector = paramString;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String paramString) {
		this.title = paramString;
	}

	public boolean isGridColumn() {
		return this.gridColumn;
	}

	public void setGridColumn(boolean paramBoolean) {
		this.gridColumn = paramBoolean;
	}
}